﻿using DataAccess;
using Model.Medicine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessLogic
{
    public class MedicineBL: MarshalByRefObject
    {
        private MedicineDA medicineDA;
        public override object InitializeLifetimeService()
        {
            return null;// live forever;
        }

        public MedicineBL()
        {
            medicineDA = new MedicineDA();
        }

        public Medicine createMedicine()
        {
            return new Medicine();
        }
        public MedGroup createMedGroup()
        {
            return new MedGroup();
        }
        public MedicineDetail createMedDetail()
        {
            return new MedicineDetail();
        }
        public MedUnit createMedUnit()
        {
            return new MedUnit();
        }
        public RouteOfAdmin createRouteOfAdmin()
        {
            return new RouteOfAdmin();
        }

        /// <summary>
        /// Add a new medicine to database
        /// </summary>
        /// <param name="med">The new medicine to add </param>
        /// <returns>true if adding successfully, false otherwise</returns>
        public int addMedicine(Medicine med)
        {
            bool canAddMed = medicineDA.isMedUnitExist(med.getMedUnitId());
            if (!canAddMed)
                return -1;
            canAddMed = medicineDA.isMedGroupExist(med.getMedGroupId());
            if (!canAddMed)
                return -1;
            canAddMed = medicineDA.isRouteOfAdminExist(med.getRouteOfAdministrationId());
            if (!canAddMed)
                return -1;
            return medicineDA.addMedicine(med);
        }

        public bool updateMedicine(Medicine med, bool isUpdateQuantity)
        {
            int medId = med.getMedId();
            //Check if this medicine is used by some one.
            bool isMedUsed = isUsed(medId);
            if (isMedUsed)
            {
                //Update old med's status to 0 
                med.setStatus(0);
                medicineDA.updateMedicine(med, isUpdateQuantity);
                //Add a new med with medicine info from current medicine
                med.setStatus(1);
                medicineDA.addMedicine(med);
                //re update med detail for this medicine
                medicineDA.copyMedicineDetail(medicineDA.getMedDetailByMedId(med.getMedId()), med.getMedId() + 1);
                return true;
            }
            return medicineDA.updateMedicine(med, isUpdateQuantity);
        }
        public bool updateMedicineAnyWay(Medicine med)
        {
            
            return medicineDA.updateMedicineAnyWay(med);
        }

        /// <summary>
        /// Check if a medicine is used by patient
        /// </summary>
        /// <param name="medId"></param>
        /// <returns></returns>
        public bool isUsed(int medId)
        {
            return medicineDA.isUsed(medId);
        }

        /// <summary>
        /// Update new quantity
        /// </summary>
        /// <param name="medId">Id of medicine will be updated</param>
        /// <param name="quantity"> A new value will be added to existing total of quantity</param>
        /// <returns></returns>
        public bool updateQuantityTotal(int medId, int quantity)
        {
            return medicineDA.updateQuantityTotal(medId, quantity);
        }

        public int getQuanityTotal(int medId)
        {
           return medicineDA.getQuanityTotal(medId);
        }
        
        public Medicine getMedicineById(int medId)
        {
            return medicineDA.getMedicineById(medId);
        }

        public Medicine[] getAllMedicine()
        {
            return medicineDA.getAllMedicine();
        }

        /////////////////////////////////////////
        //
        // Medicine Unit
        //
        /////////////////////////////////////////

        public bool addMedUnit(MedUnit medUnit)
        {
            string name = medUnit.getMedUnitName();
            bool isEx = medicineDA.isMedUnitNameExist(name);
            if (isEx)
                return false;
            return medicineDA.addMedUnit(medUnit);

        }

        public MedUnit[] getAllMedUnit()
        {
            return medicineDA.getAllMedUnit();
        }
        /// <summary>
        /// Get medicine unit of a medicine
        /// </summary>
        /// <param name="medId">Medicine Unit Id</param>
        /// <returns></returns>
        public MedUnit getMedUnit(int medUnitId)
        {
            return medicineDA.getMedUnit(medUnitId);
        }

        public bool isMedUnitExist(int medUnitId)
        {
            return medicineDA.isMedUnitExist(medUnitId);
        }

        /////////////////////////////////////////
        //
        // Medicine Group
        //
        /////////////////////////////////////////

        public bool addMedGroup(MedGroup medGroup)
        {
            string name = medGroup.getMedGroupName();
            bool isEx = medicineDA.isMedGroupNameExist(name);
            if (isEx)
                return false;
           return medicineDA.addMedGroup(medGroup);

        }

        public MedGroup[] getAllMedGroup()
        {
            return medicineDA.getAllMedGroup();
        }

        /// <summary>
        /// Get medicine group of a medicine
        /// </summary>
        /// <param name="medGroudId">Medicine Group Id</param>
        /// <returns></returns>
        public MedGroup getMedGroup(int medGroupId)
        {
            return medicineDA.getMedGroup(medGroupId);
        }

        public bool isMedGroupExist(int medGroupId)
        {
            return medicineDA.isMedGroupExist(medGroupId);
        }
        /////////////////////////////////////////
        //
        // Route of admin
        //
        /////////////////////////////////////////

        public bool addRouteOfAdmin(RouteOfAdmin roa)
        {
            string name = roa.getMedRouteOfAdminName();
            bool isEx = medicineDA.isROANameExist(name);
            if (isEx)
                return false;
            return medicineDA.addRouteOfAdmin(roa);

        }
        public RouteOfAdmin[] getAllRouteOfAdmin()
        {
           return medicineDA.getAllRouteOfAdmin();
        }

        public RouteOfAdmin getRouteOfAdmin(int roaId)
        {
            return medicineDA.getRouteOfAdmin(roaId);
        }

        public bool isRouteOfAdminExist(int roaId)
        {
            return medicineDA.isRouteOfAdminExist(roaId);
        }

        /////////////////////////////////////////
        //
        // Medicine Detail
        //
        /////////////////////////////////////////

        public bool addMedicineDetail(MedicineDetail medDetail)
        {
            return medicineDA.addMedicineDetail(medDetail);

        }

        public bool isMedExist(int medId)
        {
            return medicineDA.isMedExist(medId);
        }

        public bool isManufacturerExist(int manuId)
        {
           return medicineDA.isManufacturerExist(manuId);
        }

        public MedicineDetail[] getMedDetailByMedId(int medId)
        {
            return medicineDA.getMedDetailByMedId(medId);
        }
        public MedicineDetail getMedDetailByMedDetailId(int medDetailId)
        {
            return medicineDA.getMedDetailByMedDetailId(medDetailId);
        }
    
    }
}
